"""
Description:
Author: 月间
Date: 2025-08-16 21:36:21
LastEditTime: 2025-11-09 20:23:01
LastEditors:
"""

from tortoise.models import Model
from tortoise import fields
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .admin_power import Power
    from .admin_dept import Dept


class Role(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, description="角色名称")
    code = fields.CharField(max_length=255, description="角色标识")
    # 数据范围（0 仅本人数据权限 1 本部门数据权限 2 本部门及以下数据权限 3 自定义数据权限 4 全部数据权限）
    data_scope = fields.IntField(description="数据权限范围", default=0)
    enable = fields.IntField(description="是否启用", default=1)
    remark = fields.CharField(max_length=255, description="备注", null=True)
    details = fields.CharField(max_length=255, description="详情", null=True)
    sort = fields.IntField(description="排序", default=0)
    create_time = fields.DatetimeField(auto_now_add=True, description="创建时间")
    update_time = fields.DatetimeField(auto_now=True, description="更新时间")
    powers: fields.ManyToManyRelation["Power"] = fields.ManyToManyField(
        "models.Power",
        through="admin_role_power",
    )
    # 部门关联
    depts: fields.ManyToManyRelation["Dept"] = fields.ManyToManyField(
        "models.Dept",
        through="admin_role_dept",
    )

    # 关系将在使用时通过 Tortoise 的关系定义方式建立

    class Meta:
        table = "admin_role"
